//
//  NewBaseAlertView.m
//  Yanwei
//
//  Created by Believer Just on 2017/7/26.
//  Copyright © 2017年 DCloud. All rights reserved.
//

#import "NewBaseAlertView.h"
#import "UIView+Util.h"
#import "NSString+Util.h"
#import "UILabel+Additions.h"

#define autoWidthProportion ScreenWidth /375

@interface NewBaseAlertView ()

@property (nonatomic, strong) UIView *bgView; // 背景视图
@property (nonatomic, strong) UILabel *titleLabel; // 标题
@property (nonatomic, strong) UITextView *distructTextView; // 描述
@property (nonatomic, strong) UILabel *distructLabel; // 描述
@property (nonatomic, strong) UIView *horizontalLineView; // 水平线视图
@property (nonatomic, strong) UIView *verticalLineView; // 垂直线视图
@property (nonatomic, strong) UIButton *cancelButton; // 取消按钮
@property (nonatomic, strong) UIButton *sureButton; // 确认按钮

@property (nonatomic, assign) NewBaseAlertViewType alertType;

@end

@implementation NewBaseAlertView

- (instancetype)initWithFrame:(CGRect)frame Message:(NSString *)message Distruction:(NSString *)distruction CancelButtonTitle:(NSString *)cancelButtonTitle SureButtonTitle:(NSString *)sureButtonTitle ButtonNum:(NSInteger)buttonNum alertType:(NewBaseAlertViewType)alertType {
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化视图
        [self initView:message Distruction:distruction CancelButtonTitle:cancelButtonTitle SureButtonTitle:sureButtonTitle ButtonNum:buttonNum alertType:alertType];
    }
    return self;
}

- (void)setDistruction:(NSString *)distruction {
    _distruction = distruction;
    self.distructTextView.text = distruction;
    self.distructLabel.text = distruction;
    [UILabel changeLineSpaceAndCenterForLabel:self.distructLabel WithSpace:4];
    [self.distructTextView layoutIfNeeded];
    [self updateBgViewFrameWithMessage];
}
- (void)setDistructionAttributedString:(NSString *)distructionAttributedString {
    _distructionAttributedString = distructionAttributedString;
    
    NSData *dataContent = [_distructionAttributedString dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:dataContent options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];
    
    if (self.alertType == NewBaseAlertViewCouponVoucherType) {
        NSMutableAttributedString *attributedStrings = [[NSMutableAttributedString alloc] initWithString:_distructionAttributedString];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
        style.alignment = NSTextAlignmentCenter;
        [attributedStrings addAttribute:NSFontAttributeName
                                 value:APPFONT(14)
                                 range:NSMakeRange(0, attributedStrings.length)];
        [attributedStrings addAttribute:NSParagraphStyleAttributeName
                                 value:style
                                 range:NSMakeRange(0, attributedStrings.length)];
        
        NSDictionary *rangBeforedic = @{NSForegroundColorAttributeName:Color34};
        NSDictionary *rangCenterdic = @{NSForegroundColorAttributeName:Color0108183};
        NSDictionary *rangAfterdic = @{NSForegroundColorAttributeName:Color34};
        [attributedStrings addAttributes:rangBeforedic range:NSMakeRange(0, 12)];
        [attributedStrings addAttributes:rangCenterdic range:NSMakeRange(12, _distructionAttributedString.length -12 -10)];
        [attributedStrings addAttributes:rangAfterdic range:NSMakeRange(_distructionAttributedString.length -10, 10)];
        self.distructTextView.attributedText = attributedStrings;
        self.distructLabel.attributedText = attributedStrings;
        [self.distructTextView layoutIfNeeded];
        [self updateBgViewFrameWithMessage];
        return;
    } else if (self.alertType == NewBaseAlertViewVouchersType) {
        // 设置字体和设置字体的范围
        [attributedString addAttribute:NSFontAttributeName
                                 value:APPFONT(14)
                                 range:NSMakeRange(0, attributedString.length)];
        // 添加文字颜色
        [attributedString addAttribute:NSForegroundColorAttributeName
                                 value:Color80
                                 range:NSMakeRange(0, attributedString.length)];
    } else {
        // 设置字体和设置字体的范围
        [attributedString addAttribute:NSFontAttributeName
                        value:APPFONT(14)
                        range:NSMakeRange(0, attributedString.length)];
        // 添加文字颜色
        [attributedString addAttribute:NSForegroundColorAttributeName
                        value:Color34
                        range:NSMakeRange(0, attributedString.length)];
    }

    self.distructTextView.attributedText = attributedString;
    self.distructLabel.attributedText = attributedString;
    [self.distructTextView layoutIfNeeded];
    [self updateBgViewFrameWithMessage];
}

#pragma mark - method
/** 初始化视图 */
- (void)initView:(NSString *)message Distruction:(NSString *)distruction CancelButtonTitle:(NSString *)cancelButtonTitle SureButtonTitle:(NSString *)sureButtonTitle ButtonNum:(NSInteger)buttonNum alertType:(NewBaseAlertViewType)alertType {
    self.alertType = alertType;
    switch (buttonNum) {
        case 1:
        case 2:
            // 背景视图
            self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 282 *autoWidthProportion, 166 *autoWidthProportion)];
            self.bgView.centerX = self.centerX;
            self.bgView.centerY = self.centerY;
            self.bgView.backgroundColor = [UIColor whiteColor];
            self.bgView.cornerRadius = 5;
            [self addSubview:self.bgView];
            [self.bgView layoutIfNeeded];
            break;
        case 3:
            if (ScreenWidth >320) {
                self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 282 *autoWidthProportion, 190 *autoWidthProportion)];
                self.bgView.centerX = self.centerX;
                self.bgView.centerY = self.centerY;
                self.bgView.backgroundColor = [UIColor whiteColor];
                self.bgView.cornerRadius = 5;
                [self addSubview:self.bgView];
                [self.bgView layoutIfNeeded];
            } else {
                self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 282, 190)];
                self.bgView.centerX = self.centerX;
                self.bgView.centerY = self.centerY;
                self.bgView.backgroundColor = [UIColor whiteColor];
                self.bgView.cornerRadius = 5;
                [self addSubview:self.bgView];
                [self.bgView layoutIfNeeded];
            }
            break;
        default:
            break;
    }
    
    // 标题
    self.titleLabel = ({ [UILabel new]; });
    if (message.hasValue) {
        self.titleLabel.text = message;
    } else {
        self.titleLabel.text = @"温馨提示";
    }
    self.titleLabel.textColor = Color34;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).with.offset(24 *autoWidthProportion);
        make.left.and.right.equalTo(self.bgView);
        
        if ([message isEqualToString:@"placeholder"]) {
            make.height.mas_equalTo(0);
        } else {
            make.height.mas_equalTo(15 *autoWidthProportion);
        }
    }];
    
    
    // 水平线视图
    self.horizontalLineView = ({ [UIView new]; });
    self.horizontalLineView.backgroundColor = Color217;
    [self.bgView addSubview:self.horizontalLineView];
    [self.horizontalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bgView).with.offset(- 48 *autoWidthProportion);
        make.left.and.right.equalTo(self.bgView);
        make.height.mas_equalTo(0.5);
    }];
    
    // 描述
    self.distructTextView = ({ [UITextView new]; });
    if (distruction.hasValue) {
        self.distructTextView.text = distruction;
    } else {
        self.distructTextView.text = @"提示内容";
    }
    self.distructTextView.textAlignment = NSTextAlignmentCenter;
    self.distructTextView.font = APPFONT(14);
    [self.distructTextView setEditable:NO];
    [self.bgView addSubview:self.distructTextView];
    [self.distructTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(21 *autoWidthProportion);
        make.left.equalTo(self.bgView).with.offset(30 *autoWidthProportion);
        make.right.equalTo(self.bgView).with.offset(- 30 *autoWidthProportion);
        make.bottom.equalTo(self.horizontalLineView.mas_top).with.offset(- 21 *autoWidthProportion);
    }];
    
    self.distructLabel = [UILabel new];
    if (distruction.hasValue) {
        self.distructLabel.text = distruction;
    } else {
        self.distructLabel.text = @"提示内容";
    }
    self.distructLabel.numberOfLines = 0;
    self.distructLabel.textAlignment = NSTextAlignmentCenter;
    self.distructLabel.font = APPFONT(14);
    [self.bgView addSubview:self.distructLabel];
    [self.distructLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(21 *autoWidthProportion);
        make.left.equalTo(self.bgView).with.offset(30 *autoWidthProportion);
        make.right.equalTo(self.bgView).with.offset(- 30 *autoWidthProportion);
        make.bottom.equalTo(self.horizontalLineView.mas_top).with.offset(- 21 *autoWidthProportion);
    }];
    
    
    switch (buttonNum) {
        case 1: {
            self.titleLabel.font = APPFONT(15);
            self.distructTextView.textColor = Color34;
            self.distructLabel.textColor = Color34;
            // 取消按钮
            self.cancelButton = ({ [UIButton buttonWithType:UIButtonTypeCustom]; });
            if (cancelButtonTitle.hasValue) {
                [self.cancelButton setTitle:cancelButtonTitle forState:UIControlStateNormal];
            } else {
                [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            }
            [self.cancelButton setTitleColor:Color175030 forState:UIControlStateNormal];
            self.cancelButton.titleLabel.font = APPFONT(15);
            self.cancelButton.tag = NewBAVCancelBtnType;
            [self.cancelButton addTarget:self action:@selector(progressButton:) forControlEvents:UIControlEventTouchUpInside];
            [self.bgView addSubview:self.cancelButton];
            [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.horizontalLineView.mas_bottom);
                make.left.and.bottom.equalTo(self.bgView);
                make.centerX.equalTo(self.bgView.mas_centerX);
            }];
            if (alertType == NewBaseAlertViewLimitBuyingType) {
                [self.cancelButton setTitleColor:Color34 forState:UIControlStateNormal];
            }
            break;
        }
        case 2: {
            self.titleLabel.font = APPBOLDPINGFANGFONT(16);
            self.distructTextView.textColor = RGBACOLOR(100, 100, 100, 1);
            self.distructLabel.textColor = RGBACOLOR(100, 100, 100, 1);
            // 取消按钮
            self.cancelButton = ({ [UIButton buttonWithType:UIButtonTypeCustom]; });
            if (cancelButtonTitle.hasValue) {
                [self.cancelButton setTitle:cancelButtonTitle forState:UIControlStateNormal];
            } else {
                [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            }
            [self.cancelButton setTitleColor:Color34 forState:UIControlStateNormal];
            self.cancelButton.titleLabel.font = APPFONT(14);
            self.cancelButton.tag = NewBAVCancelBtnType;
            [self.cancelButton addTarget:self action:@selector(progressButton:) forControlEvents:UIControlEventTouchUpInside];
            [self.bgView addSubview:self.cancelButton];
            [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.horizontalLineView.mas_bottom);
                make.left.and.bottom.equalTo(self.bgView);
                make.right.equalTo(self.bgView.mas_centerX).with.offset(- 0.25);
            }];
            
            // 垂直线视图
            self.verticalLineView = ({ [UIView new]; });
            self.verticalLineView.backgroundColor = Color217;
            [self.bgView addSubview:self.verticalLineView];
            [self.verticalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.cancelButton.mas_right);
                make.width.mas_equalTo(0.5);
                make.top.and.bottom.equalTo(self.cancelButton);
            }];
            
            // 立即更新按钮
            self.sureButton = ({ [UIButton buttonWithType:UIButtonTypeCustom]; });
            if (sureButtonTitle.hasValue) {
                [self.sureButton setTitle:sureButtonTitle forState:UIControlStateNormal];
            } else {
                [self.sureButton setTitle:@"确认" forState:UIControlStateNormal];
            }
            if (alertType == NewBaseAlertViewCouponVoucherType || alertType == NewBaseAlertViewZGType || alertType == NewBaseAlertViewVouchersType) {
                [self.sureButton setTitleColor:Color0108183 forState:UIControlStateNormal];
            } else if (alertType == NewBaseAlertViewRefoundType) {
                [self.sureButton setTitleColor:Color175030 forState:UIControlStateNormal];
            }  else {
                [self.sureButton setTitleColor:Color34 forState:UIControlStateNormal];
            }
            self.sureButton.titleLabel.font = APPFONT(14);
            self.sureButton.tag = NewBAVSureBtnType;
            [self.sureButton addTarget:self action:@selector(progressButton:) forControlEvents:UIControlEventTouchUpInside];
            [self.bgView addSubview:self.sureButton];
            [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.horizontalLineView.mas_bottom);
                make.right.and.bottom.equalTo(self.bgView);
                make.left.equalTo(self.bgView.mas_centerX).with.offset(0.25);
            }];
            break;
        }
        case 3: {
            self.titleLabel.font = APPBOLDPINGFANGFONT(15);
            self.distructTextView.textColor = Color34;
            self.distructLabel.textColor = Color34;
            // 取消按钮
            self.cancelButton = ({ [UIButton buttonWithType:UIButtonTypeCustom]; });
            if (cancelButtonTitle.hasValue) {
                [self.cancelButton setTitle:cancelButtonTitle forState:UIControlStateNormal];
            } else {
                [self.cancelButton setTitle:@"确认" forState:UIControlStateNormal];
            }
            [self.cancelButton setTitleColor:Color34 forState:UIControlStateNormal];
            self.cancelButton.titleLabel.font = APPFONT(15);
            self.cancelButton.tag = NewBAVCancelBtnType;
            [self.cancelButton addTarget:self action:@selector(progressButton:) forControlEvents:UIControlEventTouchUpInside];
            [self.bgView addSubview:self.cancelButton];
            [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.horizontalLineView.mas_bottom);
                make.left.and.bottom.equalTo(self.bgView);
                make.centerX.equalTo(self.bgView.mas_centerX);
            }];
            break;
        }
        default:
            break;
    }
    
    [self updateBgViewFrameWithMessage];
}


#pragma mark - progress
- (void)progressButton:(UIButton *)sender {
    [self hideView];
    switch (sender.tag) {
        case NewBAVCancelBtnType: {
            if (self.newBaseAlertViewBlock) {
                self.newBaseAlertViewBlock(NewBAVCancelBtnType);
            }
            break;
        }
        case NewBAVSureBtnType: {
            if (self.newBaseAlertViewBlock) {
                self.newBaseAlertViewBlock(NewBAVSureBtnType);
            }
            break;
        }
        default:
            break;
    }
}

- (void)updateBgViewFrameWithMessage {
    CGFloat h = [self heightForString:self.distructTextView andWidth:(self.bgView.width - 60 * autoWidthProportion)];
    if (h < 200) {
        if (self.alertType == NewBaseAlertViewCouponVoucherType || self.alertType == NewBaseAlertViewVouchersType || self.alertType == NewBaseAlertViewRefoundType) {
            self.distructTextView.hidden = NO;
            self.distructLabel.hidden = YES;
        } else {
            self.distructTextView.hidden = YES;
            self.distructLabel.hidden = NO;
            return;
        }
    } else {
        self.distructTextView.hidden = NO;
        self.distructLabel.hidden = YES;
    }

    self.distructTextView.scrollEnabled = YES;
    CGFloat defaultHeight = 15 * autoWidthProportion + 48 *autoWidthProportion + 62 + h;
    
//    if (ScreenWidth == 375) {
     if (ScreenWidth >= 375) {
        defaultHeight *= 1.1;
    }
    if (defaultHeight > 166 *autoWidthProportion) {
        self.bgView.height = defaultHeight;
        self.bgView.centerY = self.centerY;
        [self.bgView setNeedsLayout];
        self.distructTextView.scrollEnabled = NO;
        
        if (defaultHeight >= ScreenHeight /3 *2) {
            self.bgView.height = ScreenHeight /3 *2;
            self.bgView.centerY = self.centerY;
            [self.bgView setNeedsLayout];
            self.distructTextView.scrollEnabled = YES;
        }
    }
}

- (CGFloat)heightForString:(UITextView *)textView andWidth:(float)width {
    CGSize sizeToFit = [textView sizeThatFits:CGSizeMake(width, MAXFLOAT)];
    return sizeToFit.height;
}

@end
